package volatiletest;

/**
 * @Classname VolatileTest
 * @Description TODO
 * @Date 2021/5/12 0:09
 * @Created by 张斌
 */
public class VolatileTest {
    public static volatile int race = 0;
    public static final int THREAD_COUNTS = 20;

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[THREAD_COUNTS];
        for (int i = 0; i < THREAD_COUNTS; i++) {
            threads[i] = new Thread(new Runnable() {
                public void run() {
                    for (int j = 0; j < 10000; j++) {
                        increse();
                    }
                }
            });
            threads[i].setName("thread_volatile"+i);
            threads[i].start();
        }

        while (Thread.activeCount() > 1) {
            //System.out.println(Thread.activeCount());
            //System.out.println(Thread.);
            Thread.yield();

        }

        System.out.println(race);
    }

    public static void increse() {
        race++;
    }


}




